package com.rzico.sbl.ui.report.manager

import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportStationBinding
import com.rzico.sbl.model.ManagerStation
import com.rzico.sbl.other.getBeforeDate
import com.rzico.sbl.other.getEndDate
import com.rzico.sbl.other.getFirstDate
import com.rzico.sbl.other.getTodayDate
import com.rzico.sbl.ui.report.driver.DriverStationMapActivity
import com.rzico.sbl.ui.report.other.ReportUrl
import com.rzico.sbl.viewmodel.ReportStationViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.listener.onTabSelectedListener
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.glideExt.loadRoundedImage
import com.xinnuo.slimadapter.SlimAdapter
import org.json.JSONObject

class ReportStationActivity : BaseActivity(R.layout.activity_report_station) {

    private val mBinding by lazy { ActivityReportStationBinding.bind(getRootView()) }

    private var mDateHint = ""
    private var mBeginDate = ""
    private var mEndDate = ""

    override fun getViewModel() = get<ReportStationViewModel>()

    override fun initData() {
        setToolbarVisibility(false)

        initLayout()
        initTabLayout()
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关排名信息！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = {
                            getTotalData()
                            getData()
                        }
                    )

                    view.addItemDecoration(SpaceTBDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<ManagerStation>(R.layout.item_station_rank) { jector, bean, index ->
                            jector.apply {
                                text(R.id.item_station_title, bean.name)
                                text(R.id.item_station_name, bean.linkman.orEmpty("无"))
                                text(R.id.item_station_phone, bean.phone)
                                text(R.id.item_station_sale, bean.subQuantity)

                                with<ImageView>(R.id.item_station_img) {
                                    it.loadRoundedImage(bean.logo, 10f)
                                }

                                with<ImageView>(R.id.item_station_rank) {
                                    it.isVisible = index < 3
                                    when (index) {
                                        0 -> it.setImageResource(R.mipmap.icon_station02)
                                        1 -> it.setImageResource(R.mipmap.icon_station03)
                                        2 -> it.setImageResource(R.mipmap.icon_station04)
                                    }
                                }

                                with<TextView>(R.id.item_station_level) {
                                    it.isVisible = index > 2
                                    it.text = "${index + 1}"
                                }

                                clicked {
                                    startActivityEx<ReportStationDetailActivity>(
                                        "memberId" to bean.member,
                                        "logo" to bean.logo.orEmpty(),
                                        "title" to bean.name.orEmpty(),
                                        "name" to bean.linkman.orEmpty("无"),
                                        "phone" to bean.phone.orEmpty()
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    /**
     * TabLayout 初始化
     */
    private fun initTabLayout() {
        mBinding
            .stationTab
            .apply {
                onTabSelectedListener {
                    onTabSelected {
                        it?.apply {
                            when (position) {
                                0 -> {
                                    mDateHint = "近7天"
                                    mBeginDate = getBeforeDate(6)
                                    mEndDate = getTodayDate()
                                }
                                1 -> {
                                    mDateHint = "本月"
                                    mBeginDate = getFirstDate()
                                    mEndDate = getEndDate()
                                }
                            }

                            getViewModel().cancelRequest()
                            getViewModel().delay { updateList() }
                            getTotalData()
                        }
                    }
                }

                addTab(this.newTab().setText("近7天"), true)
                addTab(this.newTab().setText("本月"), false)
            }
    }

    override fun initListener() {
        mBinding
            .apply {
                stationLay.oneClick {
                    startActivityEx<DriverStationMapActivity>()
                }

                stationAll.oneClick {
                    startActivityEx<ReportStationListActivity>(
                        "dateHint" to mDateHint,
                        "beginDate" to mBeginDate,
                        "endDate" to mEndDate
                    )
                }
            }
    }

    /**
     * 获取总合计
     */
    private fun getTotalData() {
        getViewModel()
            .request(
                isLoadingBefore = false,
                isLoadingAfter = false,
                requestUrl = ReportUrl.getBaseUrl().zStasum,
                requestMethod = HttpMethod.GET,
                params = arrayOf(
                    "beginDate" to mBeginDate,
                    "endDate" to mEndDate
                ),
                onSuccess = {
                    val obj = JSONObject(it)

                    mBinding.apply {
                        stationTotal.text = obj.optNotNull("n1")
                        stationSale.text = obj.optNotNull("n2")
                    }
                }
            )
    }

    /**
     * 水站列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .stationList(
                page = 0,
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = {
                    mList.clear()
                    mList.addItems(it)

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding
                        .listLayout
                        .swipeRefresh
                        .isRefreshing = false
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        mBinding
            .listLayout
            .emptyLayout
            .emptyView
            .gone()

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData()
    }

}